#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cassert>
#include <vector>
#include <cstdlib>

using namespace std; 

int get_cplx(string s)
{
	unsigned i; 
	int ret; 
	//O(n^1), O(1)
	assert(s[0]=='O');
	assert(s[1]=='('); 
	if (s[2]=='1') {
		return 0; 
	}
	assert(s[2]=='n'); 
	assert(s[3]=='^'); 
	ret = 0; 
	for (i=4; i<s.size(); i++) {
		if (s[i]<'0' || s[i]>'9')
			break; 
		ret *= 10; 
		ret += s[i]-'0'; 
	}
	return ret; 
}

void get_tokens(string line, vector<string> &ret)
{
	string cur; 
	int spc = 1; 
	
	for (unsigned i=0; i<line.size(); i++) {
		if (spc) {
			if (line[i]==' ') {
				//do nothing
			} else {
				spc = 0; 
				cur.push_back(line[i]); 
			}
		} else {
			if (line[i]==' ') {
				spc=1; 
				ret.push_back(cur); 
				cur.assign(""); 
			} else {
				cur.push_back(line[i]); 
			}
		}
	}
	if (cur.size()>0) {
		ret.push_back(cur); 
		cur.assign(""); 
	}
}

//ol: -1 --> invalid, 0-->O(1), >0-->O(n^x)
void parse_f(string line, string &var, int &ol)
{
	vector <string> tokens; 
	string x, y; 

	get_tokens(line, tokens); 
	assert(tokens.size()==4); 

	assert(tokens[0]=="F"); 

	var = tokens[1]; 
	x = tokens[2]; 
	y = tokens[3]; 
	if (x=="n" && y=="n") {
		ol = 0; 
	} else if (x=="n" && y!="n") {
		ol = -1; 
	} else if (x!="n" && y=="n") {
		ol = 1; 
	} else {
		ol = (atoi(x.c_str())<=atoi(y.c_str()) ? 0 : -1); 
	}
}

struct flag_t {
	string var; 
	int ol; 		//Big O's level
}; 

bool var_exist(vector<flag_t> stack, string var) 
{
	for (unsigned i=0; i<stack.size(); i++) {
		if (stack[i].var==var)
			return true; 
	}
	return false;
}

int main()
{
	int t, c, m, cplx; 
	vector <flag_t>flag_stack; 
	flag_t flag; 
	string var; 
	int ol;
	
	//freopen("p3952.in", "r", stdin); 
	cin>>t; 
	for (int i=0; i<t; i++) {
		int nr_lines, err;
		string cplx_str; 
		string line; 
		
		cin>>nr_lines>>cplx_str; 
		getline(cin, line); 		// to eat the last line's '\n'

		c = 0; 
		m = 0; 
		err = 0; 
		flag_stack.clear(); 
		cplx = get_cplx(cplx_str); 

		for (int j=0; j<nr_lines; j++) {
			getline(cin, line); 
			if (err) 
				continue; 
			if (line[0]=='F') {
				parse_f(line, var, ol); 
				if (var_exist(flag_stack, var)) {
					err=1; 
					continue; 
				}
				if (ol==-1) {
					flag.ol = c; 
					flag.var = var; 
					c = -1; 
				} else {
					if (c==-1) {
						flag.ol = c; 
						flag.var = var; 
						c = -1; 
					} else {
						flag.ol = c; 
						flag.var = var; 
						c += ol; 
					}
				}
				flag_stack.push_back(flag); 
				m = max(m, c); 
			} else if (line[0]=='E') {
				if (flag_stack.size()==0) {
					err = 1; 
				} else {
					flag = flag_stack.back(); 
					flag_stack.pop_back(); 
					c = flag.ol; 
				}
			} else {
				assert(0); 
			}
		}
		if (err || flag_stack.size()!=0) {
			cout<<"ERR\n"; 
		} else {
			const char *p; 
			p = (cplx==m ? "Yes" : "No"); 
			cout<<p<<"\n"; 
		}
	}

	return 0; 
}


